﻿
// SPDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "SP.h"
#include "SPDlg.h"
#include "afxdialogex.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CSPDlg 对话框



CSPDlg::CSPDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SP_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_SAVE, m_listSave);
}

BEGIN_MESSAGE_MAP(CSPDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CSPDlg::On32771)
	ON_BN_CLICKED(IDC_CHECK_AUTO, &CSPDlg::OnBnClickedCheckAuto)
	ON_BN_CLICKED(IDC_BUTTON_HOME, &CSPDlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_BUTTON_HOME1, &CSPDlg::OnBnClickedButtonHome1)
	ON_BN_CLICKED(IDC_BUTTON_HOME2, &CSPDlg::OnBnClickedButtonHome2)
	ON_COMMAND(ID_32777, &CSPDlg::On32777)
	ON_COMMAND(ID_32778, &CSPDlg::On32778)
	ON_COMMAND(ID_32779, &CSPDlg::On32779)
	ON_COMMAND(ID_32780, &CSPDlg::On32780)
	ON_COMMAND(ID_32781, &CSPDlg::On32781)
	ON_COMMAND(ID_32782, &CSPDlg::On32782)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON2, &CSPDlg::OnBnClickedButton2)
	ON_WM_SIZE()
	ON_COMMAND(ID_32783, &CSPDlg::On32783)
	ON_BN_CLICKED(IDC_BUTTON3, &CSPDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON5, &CSPDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CSPDlg::OnBnClickedButton6)
	ON_COMMAND(ID_32784, &CSPDlg::On32784)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_SAVE, &CSPDlg::OnLvnItemchangedListSave)
END_MESSAGE_MAP()


// CSPDlg 消息处理程序

BOOL CSPDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();


	unique_mutex = CreateMutex(NULL, FALSE, L"SPDlg");//多开检测
	DWORD errCode = GetLastError();
	if (errCode == ERROR_ALREADY_EXISTS) {
		Sleep(500);
		CloseHandle(unique_mutex);
		MessageBox(L"软件已经被打开!", L"Warning", 0);
		::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
	}
	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标


	

	Manager.GeneralSettingIniInit();
	SetDlgItemTextW(IDC_STATIC_113, Manager.m_sGeneralSetting.m_cstrProjectName);

	// TODO: 在此添加额外的初始化代码
	Manager.LoginConfigIniInit();
	Manager.TypeConfigIniInit();
	Manager.ReadTypeConfigIni();
	SetType(Manager.TypeNum);
	
	Manager.StartRS232LinkThread();
	m_menu = this->GetMenu();

	SetUiHandle();

	Manager.m_cMainDlg = this;
	
	Manager.m_ptrMainUI = this;
	m_LogDlg = new CLogDlg();
	m_LogDlg->Create(IDD_DLG_LOG,this);
	m_LogDlg->ShowWindow(SW_HIDE);
	
	Manager.SetLogUI(m_LogDlg->GetSafeHwnd());

	if (Manager.GetCMotor()->Init())
	{
		Sleep(1000);
		Manager.GetCMotor()->ReadConfigFile();
		Sleep(1000);
		Manager.GetCio()->StartGetIOInputThread();
		//Manager.GetCMotor()->StartGetMotorIOThread();
		Manager.GetCMotor()->StartGetMotorEncoderThread();
	}
	Sleep(3000);
	this->ShowWindow(SW_SHOWMAXIMIZED);
	//Manager.LightCheck();
	Manager.GetCio()->SetOutPutIO(11,ON);
	Manager.GetCio()->SetOutPutIO(14, OFF);
	Manager.GetCio()->SetOutPutIO(15, ON);

	m_IODlg = new CIODlg();
	m_IODlg->Create(IDD_DLG_IO, this);
	m_IODlg->ShowWindow(SW_HIDE);

	

	m_MotorDlg = new CMotorDlg();
	m_MotorDlg->Create(IDD_DIALOG_MOTOR, this);
	m_MotorDlg->ShowWindow(SW_HIDE);
	

	m_ControlDlg= new CControlDlg();
	m_ControlDlg->Create(IDD_DLG_CONTROL, this);
	m_ControlDlg->ShowWindow(SW_HIDE);

	Manager.ReadComConfig();
	m_ComDlg = new CComDlg();
	m_ComDlg->Create(IDD_DLG_COM, this);
	m_ComDlg->ShowWindow(SW_HIDE);

	Manager.MesConfigIniInit();
	Manager.CountConfigInit();
	Manager.ReadPosConfig();
	Manager.ShowInfoThread();
	Manager.StartScanBtnVal();
	
	Manager.ScrewCheckThread();
	Manager.StartLightControlThread();
	Manager.StartAutoScrewThread();

	{
		if (m_cLoginDlg != nullptr)
		{
			m_cLoginDlg->ShowWindow(SW_SHOW);
		}
		if (m_cLoginDlg == nullptr)
		{
			m_cLoginDlg = new LoginDialog();
			m_cLoginDlg->Create(IDD_DIALOG_LOGIN, this);
		}
		m_cLoginDlg->ShowWindow(SW_SHOW);
	}

	m_CSetDlg = new CSettingDlg();
	m_CSetDlg->Create(IDD_DLG_SETTING, this);
	m_CSetDlg->ShowWindow(SW_HIDE);

	m_TcpIPDlg = new TcpIpDlg();
	m_TcpIPDlg->Create(IDD_DLG_TCPIP, this);
	m_TcpIPDlg->ShowWindow(SW_HIDE);

	{
		LONG lStyle;
		m_listSave.SetBkColor(RGB(200, 220, 250));
		//m_logList.SetBkColor(TRANSPARENT);
		//m_logList.SetTextColor(RGB(250, 250, 250));
		lStyle = GetWindowLong(m_listSave.m_hWnd, GWL_STYLE);
		lStyle &= ~LVS_TYPEMASK;
		lStyle |= LVS_REPORT;
		SetWindowLong(m_listSave.m_hWnd, GWL_STYLE, lStyle);
		m_listSave.ShowWindow(SW_SHOW);
		DWORD dwStyle = m_listSave.GetExtendedStyle();
		dwStyle |= LVS_EX_FULLROWSELECT;
		dwStyle |= LVS_EX_GRIDLINES;
		dwStyle |= LVS_EX_INFOTIP;
		CFont                      m_logFont;
		double m_fScreenX = 1;
		m_logFont.CreateFont(int(30 * m_fScreenX), 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
			ANSI_CHARSET,
			OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			DEFAULT_QUALITY,
			DEFAULT_PITCH | FF_SWISS,
			_T("Arial"));
		m_listSave.SetExtendedStyle(dwStyle);
		m_listSave.SetFont(&m_logFont);
		m_listSave.EnableWindow(TRUE);
		m_listSave.InsertColumn(0,Manager.TiaomaName1.GetBuffer(), LVCFMT_LEFT, 200);
		m_listSave.InsertColumn(1, Manager.TiaomaName2.GetBuffer(), LVCFMT_LEFT, 200);
		m_listSave.InsertColumn(2, L"开始时间", LVCFMT_LEFT, 90);
		m_listSave.InsertColumn(3, L"结束时间", LVCFMT_LEFT, 90);
		m_listSave.InsertColumn(4, L"结果", LVCFMT_LEFT, 50);
		m_listSave.InsertColumn(5, L"操作员", LVCFMT_LEFT, 30);
		m_listSave.InsertColumn(6, L"产品型号", LVCFMT_LEFT, 30);
		m_listSave.InsertColumn(7, L"载具型号", LVCFMT_LEFT, 30);
		m_listSave.InsertColumn(8, L"程序", LVCFMT_LEFT, 30);
		m_listSave.InsertColumn(9, L"实际扭力", LVCFMT_LEFT, 90);
		m_listSave.InsertColumn(10, L"步数", LVCFMT_LEFT, 90);
	}
	Manager.m_vCMotor[MOTOR_X].ServoOn();
	Manager.m_vCMotor[MOTOR_Y].ServoOn();
	Manager.m_vCMotor[MOTOR_Z].ServoOn();

	

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CSPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CSPDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CSPDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
void CSPDlg::OnCancel()
{
	Manager.m_nExitFlag = 1;
	Sleep(200);

	//还要加停止所有轴，锁紧刹车
	
	/*KillTimer(TIMER_ID_IO_CONTROL_STATUS);
	KillTimer(TIMER_CONTROL_MOTOR_POSITION);
	KillTimer(TIMER_ID_AXIS_STATUS);*/
	dmc_emg_stop(Manager.m_Card);
	
	m_LogDlg->DestroyWindow();
	m_IODlg->DestroyWindow();
	m_MotorDlg->DestroyWindow();
	m_ControlDlg->DestroyWindow();
	m_ComDlg->DestroyWindow();
	m_CSetDlg->DestroyWindow();
	delete m_LogDlg;
	delete m_IODlg;
	delete m_MotorDlg;
	delete m_ControlDlg;
	delete m_ComDlg;
	delete m_CSetDlg;
	dmc_board_close();
	CDialogEx::OnCancel();
}

void CSPDlg::On32771()
{
	// TODO: 在此添加命令处理程序代码
	m_LogDlg->ShowWindow(SW_SHOW);
}

void CSPDlg::OnBnClickedCheckAuto()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_AUTO);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		Manager.m_bAutoFlag_1 = 1;
		cstr.Append(L"打螺丝开启自动");
		Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_AUTO)->SetWindowTextW(L"打螺丝自动开启");
		//GetDlgItem(IDC_CHECK_AUTO)->GetDC();
	}
	if (nFlag == BST_UNCHECKED)
	{
		Manager.m_bAutoFlag_1 = 0;
		cstr.Append(L"打螺丝关闭自动");
		Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_AUTO)->SetWindowTextW(L"打螺丝自动关闭");
	}
}


void CSPDlg::OnBnClickedButtonHome()
{

	// TODO: 在此添加控件通知处理程序代码
	Manager.GetLog()->DisplayAndSaveLog(L"按下回原点按钮", LV_EVENT);
	if (Manager.m_bHomeFlag == 0)
		Manager.SwitchTask(Manager.m_nTaskSelect);
		Manager.m_bButtonVal = 10;
}


void CSPDlg::OnBnClickedButtonHome1()
{
	// TODO: 在此添加控件通知处理程序代码
	if (IDYES == AfxMessageBox(L"确定 清空当前统计信息 吗？", MB_YESNO))
	{
		CString cstr;
		cstr.Format(L"统计信息: 总数:%d  OK数:%d", Manager.TotalCount, Manager.OKCnt);
		Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_ERROR);
		Manager.TotalCount = 0;
		Manager.OKCnt = 0;
		Manager.NGCnt = 0;
	}
}


void CSPDlg::OnBnClickedButtonHome2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (IDYES == AfxMessageBox(L"确定 清空批头使用次数 吗？", MB_YESNO))
	{
		Manager.m_nPitouCount = 0;
	}
}


void CSPDlg::On32777()
{
	// TODO: 在此添加命令处理程序代码
	m_IODlg->ShowWindow(SW_SHOW);
}


void CSPDlg::On32778()
{
	// TODO: 在此添加命令处理程序代码
	m_MotorDlg->ShowWindow(SW_SHOW);
}


void CSPDlg::On32779()
{
	// TODO: 在此添加命令处理程序代码
	m_ControlDlg->ShowWindow(SW_SHOW);
}


void CSPDlg::On32780()
{
	// TODO: 在此添加命令处理程序代码
	m_CSetDlg->ShowWindow(SW_SHOW);
}


void CSPDlg::On32781()
{
	// TODO: 在此添加命令处理程序代码
	m_ComDlg->ShowWindow(SW_SHOW);
}




void CSPDlg::On32782()
{
	// TODO: 在此添加命令处理程序代码
	m_TcpIPDlg->ShowWindow(SW_SHOW);
}


HBRUSH CSPDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr1 = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	DeleteObject(hbr);

	UINT id = pWnd->GetDlgCtrlID();
	CString Cstr;
	if (id == IDC_CHECK_AUTO)
	{
		if (Manager.m_bAutoFlag_1) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_55)
	{
		if (Manager.ComScrewConnectFlag ==1)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_57)
	{
		if (Manager.m_bHomeFlag == 1)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}

		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_56)
	{
		if (!Manager.m_bMotorErrorFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	/*else if (id == IDC_STATIC_15 || id == IDC_STATIC_16 || id == IDC_STATIC_17 || id == IDC_STATIC_18 || id == IDC_STATIC_19
		|| id == IDC_STATIC22 || id == IDC_STATIC_21 || id == IDC_STATIC_22 || id == IDC_STATIC_23 || id == IDC_STATIC_24)
	{
		int i = id - IDC_STATIC_15;
		if (gSystemMgr.LuosiMax > i) {
			if (gSystemMgr.LuosiCnt > i) {
				if (gSystemMgr.m_cLuosiSave[i].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_25 || id == IDC_STATIC_26 || id == IDC_STATIC_27 || id == IDC_STATIC_28 || id == IDC_STATIC_29
		|| id == IDC_STATIC_30 || id == IDC_STATIC_31 || id == IDC_STATIC_32 || id == IDC_STATIC_33 || id == IDC_STATIC_34)
	{
		int i = id - IDC_STATIC_25;
		if (gSystemMgr.LuosiMax_2 > i) {
			if (gSystemMgr.LuosiCnt_2 > i) {
				if (gSystemMgr.m_cLuosiSave_2[i].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}*/
	//else if (id == IDC_STATIC_51)
	//{
	//	if (Manager.comconnectflag) {
	//		hbr = CreateSolidBrush(RGB(0, 255, 0));
	//		//pDC->SetTextColor(RGB(0, 255, 0));
	//	}
	//	else {
	//		hbr = CreateSolidBrush(RGB(255, 0, 0));
	//		//pDC->SetTextColor(RGB(255, 0, 0));
	//	}
	//	pDC->SetBkMode(TRANSPARENT);
	//}
	else if (id == IDC_STATIC_63)
	{
		hbr = CreateSolidBrush(RGB(0, 255, 0));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_64)
	{
		hbr = CreateSolidBrush(RGB(255, 0, 0));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_10STATUS)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"Locking")
		{
			hbr = CreateSolidBrush(RGB(222, 233, 20));
		}
		else if (Cstr == L"OK")
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else if (Cstr == L"NG")
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(70, 111, 166));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_10STATUS2)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		if (Cstr == L"...")
		{
			hbr = CreateSolidBrush(RGB(157, 187, 210));
		}
		else if (Cstr == L"吸螺丝异常")
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LUOSI_1)
	{
		if (Manager.m_nLockFlag[0]) {
			if (Manager.LuosiCnt > 0) {
				if (Manager.m_cLuosiSave[0].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LUOSI_2)
	{
		if (Manager.m_nLockFlag[1]) {
			if (Manager.LuosiCnt > 1) {
				if (Manager.m_cLuosiSave[1].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LUOSI_3)
	{
		if (Manager.m_nLockFlag[2]) {
			if (Manager.LuosiCnt > 2) {
				if (Manager.m_cLuosiSave[2].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_LUOSI_4)
	{
		if (Manager.m_nLockFlag[3]) {
			if (Manager.LuosiCnt > 3) {
				if (Manager.m_cLuosiSave[3].m_nResult == 1) {
					hbr = CreateSolidBrush(RGB(0, 255, 0));
				}
				else {
					hbr = CreateSolidBrush(RGB(255, 0, 0));
				}
			}
			else {
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		else {
			hbr = CreateSolidBrush(RGB(169, 179, 195));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	}
	hbr1 = hbr;
	return hbr1;
}


void CSPDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_cUserCheckDlg != nullptr)
	{
		delete m_cUserCheckDlg;
		m_cUserCheckDlg = nullptr;
	}
	if (m_cUserCheckDlg == nullptr)
	{
		m_cUserCheckDlg = new CUserCheckDlg;
		m_cUserCheckDlg->Create(IDD_DIALOG_USER, this);
	}
	m_cUserCheckDlg->ShowWindow(SW_SHOW);
}

BOOL CSPDlg::PreTranslateMessage(MSG * pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE) return TRUE; //如果是空格消息，返回；
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE; //如果是Esc消息，返回；
	if (Manager.m_nGetCodeFlag == 0 && pMsg->message == WM_KEYDOWN) {
		USES_CONVERSION;
		if (pMsg->wParam == VK_RETURN && Manager.m_nBarcodeLength > 0)
		{
			Manager.GetLog()->DisplayAndSaveLog(A2T(Manager.Barcode), LV_EVENT);
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(Manager.Barcode));
			if (Manager.SNLengthCheck == 1) {
				if (Manager.m_nBarcodeLength == Manager.PCBSNLength) {
					Manager.m_nGetCodeFlag = 1;
				}
				else {
					CString cstr;
					cstr.Format(L"PCB码长度错误:[%d],%s", Manager.m_nBarcodeLength, A2T(Manager.Barcode));
					Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);

					SetDlgItemTextW(IDC_STATIC_10SN, L"...");
					Manager.m_nGetCodeFlag = 0;
					memset(Manager.Barcode, 0, sizeof(Manager.Barcode));
					Manager.m_nBarcodeLength = 0;
				}
			}
			else
				Manager.m_nGetCodeFlag = 1;
			return true;
		}

		if (Manager.m_nBarcodeLength >= 50) {
			Manager.m_nGetCodeFlag = -1;
			return true;
		}
		switch (pMsg->wParam)
		{
		case VK_A:
		case VK_B:
		case VK_C:
		case VK_D:
		case VK_E:
		case VK_F:
		case VK_G:
		case VK_H:
		case VK_I:
		case VK_J:
		case VK_K:
		case VK_L:
		case VK_M:
		case VK_N:
		case VK_O:
		case VK_P:
		case VK_Q:
		case VK_R:
		case VK_S:
		case VK_T:
		case VK_U:
		case VK_V:
		case VK_W:
		case VK_X:
		case VK_Y:
		case VK_Z:
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
			Manager.Barcode[Manager.m_nBarcodeLength] = pMsg->wParam;
			Manager.m_nBarcodeLength++;
			break;
		}
		//gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
		//gSystemMgr.m_nBarcodeLength++;
		if (Manager.m_nBarcodeLength > 0)
		{
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(Manager.Barcode));
		}
	}

	if (Manager.m_nGetCodeFlag2 == 0 && Manager.m_nGetCodeFlag == 1 && pMsg->message == WM_KEYDOWN) {
		USES_CONVERSION;
		if (pMsg->wParam == VK_RETURN && Manager.m_nBarcodeLength2 > 0)
		{
			Manager.GetLog()->DisplayAndSaveLog(A2T(Manager.Barcode2), LV_EVENT);
			SetDlgItemTextW(IDC_STATIC_10SN2, A2T(Manager.Barcode2));

			if (Manager.SNLengthCheck == 1) {
				if (Manager.m_nBarcodeLength2 == Manager.QiankeSNLength) {
					Manager.m_nGetCodeFlag2 = 1;
				}
				else {
					CString cstr;
					cstr.Format(L"产品码长度错误:[%d],%s", Manager.m_nBarcodeLength2, A2T(Manager.Barcode2));
					Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);

					SetDlgItemTextW(IDC_STATIC_10SN2, L"...");
					Manager.m_nGetCodeFlag2 = 0;
					memset(Manager.Barcode2, 0, sizeof(Manager.Barcode2));
					Manager.m_nBarcodeLength2 = 0;
				}
			}
			else
				Manager.m_nGetCodeFlag2 = 1;
			return true;
		}

		if (Manager.m_nBarcodeLength2 >= 50) {
			Manager.m_nGetCodeFlag2 = -1;
			return true;
		}
		//gSystemMgr.Barcode2[gSystemMgr.m_nBarcodeLength2] = pMsg->wParam;
		//gSystemMgr.m_nBarcodeLength2++;
		switch (pMsg->wParam)
		{
		case VK_A:
		case VK_B:
		case VK_C:
		case VK_D:
		case VK_E:
		case VK_F:
		case VK_G:
		case VK_H:
		case VK_I:
		case VK_J:
		case VK_K:
		case VK_L:
		case VK_M:
		case VK_N:
		case VK_O:
		case VK_P:
		case VK_Q:
		case VK_R:
		case VK_S:
		case VK_T:
		case VK_U:
		case VK_V:
		case VK_W:
		case VK_X:
		case VK_Y:
		case VK_Z:
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
			Manager.Barcode2[Manager.m_nBarcodeLength2] = pMsg->wParam;
			Manager.m_nBarcodeLength2++;
			break;
		}
		if (Manager.m_nBarcodeLength2 > 0)
		{
			SetDlgItemTextW(IDC_STATIC_10SN2, A2T(Manager.Barcode2));
		}
	}

	if (pMsg->message == WM_KEYDOWN/* && pMsg->wParam == VK_RETURN*/) return TRUE;

	return CDialogEx::PreTranslateMessage(pMsg);
}


void CSPDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)//窗体大小发生变动。处理程序
	{
		float fsp[2];
		POINT Newp; //获取现在对话框的大小
		CRect recta;
		GetClientRect(&recta); //取客户区大小   
		Newp.x = recta.right - recta.left;
		Newp.y = recta.bottom - recta.top;
		fsp[0] = (float)Newp.x / Old.x;
		fsp[1] = (float)Newp.y / Old.y;
		CRect Rect;
		int woc;
		CPoint OldTLPoint, TLPoint; //左上角
		CPoint OldBRPoint, BRPoint; //右下角
		HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); //列出所有控件   
		while (hwndChild)
		{
			woc = ::GetDlgCtrlID(hwndChild);//取得ID
			GetDlgItem(woc)->GetWindowRect(Rect);
			ScreenToClient(Rect);
			OldTLPoint = Rect.TopLeft();
			TLPoint.x = long(OldTLPoint.x*fsp[0]);
			TLPoint.y = long(OldTLPoint.y*fsp[1]);
			OldBRPoint = Rect.BottomRight();
			BRPoint.x = long(OldBRPoint.x *fsp[0]);
			BRPoint.y = long(OldBRPoint.y *fsp[1]); //高度不可读的控件（如:combBox),不要改变此值.
			Rect.SetRect(TLPoint, BRPoint);
			GetDlgItem(woc)->MoveWindow(Rect, TRUE);
			hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
		}
		Old = Newp;
	}
	//if (0 == m_rcUiRect.left && 0 == m_rcUiRect.right && 0 == m_rcUiRect.top && 0 == m_rcUiRect.bottom)//第一次启动对话框时的大小变化不做处理
	//{
	//}
	//else
	//{
	//	if (0 == cx && 0 == cy)//如果是按下了最小化，则触发条件，这时不保存对话框数据
	//	{
	//		return;
	//	}
	//	CRect rectDlgChangeSize;
	//	GetClientRect(&rectDlgChangeSize);//存储对话框大小改变后对话框大小数据
	//}
	//GetClientRect(&m_rcUiRect); //save size of dialog
	Invalidate();//更新窗口
}


void CSPDlg::On32783()
{
	// TODO: 在此添加命令处理程序代码
	m_cLoginDlg->ShowWindow(SW_SHOW);
}


void CSPDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	if (Manager.GetCio()->Y14 == OFF) {
		Manager.GetCio()->SetOutPutIO(14, ON);
	}
	else {
		Manager.GetCio()->SetOutPutIO(14,OFF);
	}
}


void CSPDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	SetDlgItemTextW(IDC_STATIC_10SN, L"...");
	Manager.m_nGetCodeFlag = 0;
	memset(Manager.Barcode, 0, sizeof(Manager.Barcode));
	Manager.m_nBarcodeLength = 0;
}


void CSPDlg::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	SetDlgItemTextW(IDC_STATIC_10SN2, L"...");
	Manager.m_nGetCodeFlag2 = 0;
	memset(Manager.Barcode2, 0, sizeof(Manager.Barcode2));
	Manager.m_nBarcodeLength2 = 0;
}

void CSPDlg::SetUiHandle()
{
	m_menu->EnableMenuItem(ID_32777, MF_DISABLED);
	m_menu->EnableMenuItem(ID_32778, MF_DISABLED);
	m_menu->EnableMenuItem(ID_32779, MF_DISABLED);
	m_menu->EnableMenuItem(ID_32782, MF_DISABLED);
	m_menu->EnableMenuItem(ID_32780, MF_DISABLED);
	m_menu->EnableMenuItem(ID_32781, MF_DISABLED);
}

void CSPDlg::SetType(int num)
{
	switch (num)
	{
	case 1:
		Manager.TypeName = L"前后壳锁附";
		
		Manager.TiaomaName1 = L"前壳码";
		Manager.TiaomaName2 = L"PCBA码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	case 2:
		Manager.TypeName = L"支架锁附";
		Manager.TiaomaName1 = L"成品码";
		Manager.TiaomaName2 = L"支架码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	case 3:
		Manager.TypeName = L"镜头座锁附";
		Manager.TiaomaName1 = L"PCBA码";
		Manager.TiaomaName2 = L"前壳码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	case 4:
		Manager.TypeName = L"灯板锁附";
		Manager.TiaomaName1 = L"前壳码";
		Manager.TiaomaName2 = L"PCBA码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	case 5:
		Manager.TypeName = L"镜头板锁附";
		
		Manager.TiaomaName1 = L"PCBA码";
		Manager.TiaomaName2 = L"前壳码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	default:
		Manager.TypeName = L"螺丝锁附";
		Manager.TiaomaName1 = L"第一个条码";
		Manager.TiaomaName2 = L"第二个条码";
		SetDlgItemTextW(IDC_STATIC_112, Manager.TypeName);
		SetDlgItemTextW(IDC_STATIC10, Manager.TiaomaName1);
		SetDlgItemTextW(IDC_STATIC11, Manager.TiaomaName2);
		break;
	}
}



void CSPDlg::On32784()
{
	if (m_cProjectDlg != nullptr)
	{
		delete m_cProjectDlg;
		m_cProjectDlg = nullptr;
	}
	if (m_cProjectDlg == nullptr)
	{
		m_cProjectDlg = new CChangeProjectDlg;
		m_cProjectDlg->Create(IDD_DIALOG_PROJECT, this);
	}
	m_cProjectDlg->ShowWindow(SW_SHOW);
}


void CSPDlg::OnLvnItemchangedListSave(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
